<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="roleManagement">
            <el-button type="primary" size="small" @click="addRole()"
              >新增角色</el-button
            >
            <div style="margin-top: 30px">
              <el-table :data="tableData" style="width: 100%" stripe border>
                <el-table-column
                  type="index"
                  label="序号"
                  width="120"
                  align="center"
                />
                <el-table-column
                  prop="name"
                  label="角色"
                  width="240"
                  align="center"
                />
                <el-table-column
                  prop="description"
                  label="描述"
                  align="center"
                />
                <el-table-column prop="date" label="操作" align="center">
                  <template slot-scope="scope">
                    <el-button type="success" size="mini" @click="assignRole(scope.row.id)">分配权限</el-button>
                    <el-button
                      type="primary"
                      size="mini"
                      @click="editRole(scope.row.id)"
                      >编辑</el-button
                    >
                    <el-button
                      type="danger"
                      size="mini"
                      @click="delRole(scope.row.id)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </div>

            <div style="float: right; margin-top: 20px">
              <el-pagination
                @size-change="fetchData()"
                @current-change="fetchData()"
                :current-page.sync="page.page"
                :page-sizes="[10, 50, 100]"
                :page-size.sync="page.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="companyInformation">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="warning"
            />

            <el-form ref="form" label-width="80px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  v-model="formData.companyPhone"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 编辑弹层--对话框 -->
      <el-dialog
        :title="type === 'addRole' ? '新增角色' : '编辑角色'"
        :visible.sync="dialogVisible"
        @close="cancel"
      >
        <el-form ref="formRole" :model="formRole" :rules="rules">
          <el-form-item label="角色名称" label-width="100px" prop="name">
            <el-input v-model="formRole.name" />
          </el-form-item>
          <el-form-item label="角色描述" label-width="100px">
            <el-input v-model="formRole.description" type="textarea" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="sure()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分配权限弹层--对话框 -->
      <assign-role  :permission-dialog.sync="permissionDialog" ref="assignRole" />
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  delRole,
  editRole,
  getDetailRole,
  addRole,
} from "@/api/setting.js";
import { mapGetters } from "vuex";
import  assignRole  from './components/assignRole.vue';
export default {
   components: { assignRole  },
  data() {
    return {
      activeName: "roleManagement",
      page: {
        page: 1, // 页码
        pagesize: 5, // 每页条数
      },
      total: 10,
      tableData: [],
      formData: {},
      formRole: {
        name: "",
        description: "",
      },
      type: "addRole",
      dialogVisible: false,
      permissionDialog:false,
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["companyId"]),
  },
  created() {
    this.fetchData();
    this.getCompanyInfo();
  },

  methods: {
    // 获取公司角色列表所有信息
    async fetchData() {
      const { total, rows } = await getRoleList(this.page);
      console.log(this.page);
      this.tableData = rows;
      this.total = total;
    },
    // 根据id获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId);
      // console.log(this.formData)
    },
    // 删除
    async delRole(id) {
      try {
        await this.$confirm("确定删除该角色？"); // 只有点击了确定 才能进入到下方
        await delRole(id); // 调用删除接口
        this.$message.success("删除成功");
        this.fetchData();
      } catch (error) {
        console.log(error);
      }
    },
    //  点击编辑时，表单数据回显
    async editRole(id) {
      this.formRole = await getDetailRole(id);
      console.log(this.formRole)
      this.dialogVisible = true;
      this.type = "editRole";
    },
    // 新增
    addRole() {
      this.type = "addRole";
      this.dialogVisible = true;
    },
    //分配权限
    async assignRole(id){
      const {permIds}= await getDetailRole(id);
      console.log(permIds)
      // 给子组件的selectCheck赋值
      this.$refs.assignRole.selectCheck=permIds
      this.$refs.assignRole.roleId=id
      this.permissionDialog=true
    },

    // 确定按钮
    // 新增角色/编辑角色
    async sure() {
      try {
        await this.$refs.formRole.validate(); // 只有通过了表单校验才会发请求
        // formRole这个对象有id就是编辑 没有id就是新增
        if (this.formRole.id) {
          await editRole(this.formRole);
          this.$message.success("编辑成功");
        } else {
          // 新增角色
          await addRole(this.formRole);
          this.$message.success("新增成功");
        }
        this.fetchData(); // 重新拉取数据
        this.dialogVisible = false; // 关闭对话框
      } catch (error) {
        console.log(error);
      }
    },
    // 取消
    cancel() {
      this.formRole = {}; // 清空数据
      this.$refs.formRole.resetFields(); // 移除表单校验结果
      this.dialogVisible = false;
    },
  },
};
</script>

<style>
</style>
